package com.jiandaoshou;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jiandaoshou.utils.UserUtils;

public class SettingActivity extends AppCompatActivity {
    private SwitchCompat switchChat;
    private View tvExit;

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        switchChat=(SwitchCompat)findViewById(R.id.switch_chat);
        tvExit=findViewById(R.id.tvexit);

        if (UserUtils.checkIsLogin(this)) {
            tvExit.setVisibility(View.VISIBLE);
        } else {
            tvExit.setVisibility(View.GONE);
        }

        boolean is= UserUtils.getIsTiShiYin(this);
        switchChat.setChecked(is);
        switchChat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Settings.System.putInt(getContentResolver(),
                        Settings.System.SOUND_EFFECTS_ENABLED, isChecked ? 1 : 0);

                UserUtils.saveIsTishiYin(SettingActivity.this,isChecked);
            }
        });

        tvExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialDialog.Builder(SettingActivity.this)
                        .title("提示")
                        .content("确定要退出当前账号吗？")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                UserUtils.loginOut(SettingActivity.this);
                                dialog.dismiss();
                                LoginActivity.start(SettingActivity.this);
                                finish();
                            }
                        })
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
    }
}
